var ncp = require('ncp').ncp;
var fs = require('fs');
var path = require('path');

function deleteAll(path) {
    if (fs.existsSync(path)) {
        if (!fs.statSync(path).isDirectory()) { // recurse
            fs.unlinkSync(path);
            return;
        }
        var files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            var curPath = path + "/" + file;
            if (fs.statSync(curPath).isDirectory()) { // recurse
                deleteAll(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
};

deleteAll(path.resolve(__dirname, "../public/platforms"));

function copy(src, dest) {
    if (!fs.existsSync(src)) {
        console.log("copy src not exists![" + src + "]");
        return;
    }
    const dir = path.dirname(dest);
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
        console.log("copy dest not exists!created![" + dir + "]");
    }
    ncp(src, dest, err => {
        if (err !== null) {
            console.log(err);
        }
    });
}

var target = (process.argv.length > 2 ? process.argv[2] : "all").toLowerCase();
console.log("copy targets " + target);
if (target === "all" || target === "leaflet") {
    copy("./node_modules/leaflet/dist/leaflet.css", "./public/platforms/leaflet/leaflet.css");
    copy("./node_modules/leaflet/dist/images", "./public/platforms/leaflet/images");
}
